﻿/** 
 * Copyright (C) 2007-2010 Nicholas Berardi, Managed Fusion, LLC (nick@managedfusion.com)
 * 
 * <author>Nicholas Berardi</author>
 * <author_email>nick@managedfusion.com</author_email>
 * <company>Managed Fusion, LLC</company>
 * <product>Url Rewriter and Reverse Proxy</product>
 * <license>Microsoft Public License (Ms-PL)</license>
 * <agreement>
 * This software, as defined above in <product />, is copyrighted by the <author /> and the <company />, all defined above.
 * 
 * For all binary distributions the <product /> is licensed for use under <license />.
 * For all source distributions please contact the <author /> at <author_email /> for a commercial license.
 * 
 * This copyright notice may not be removed and if this <product /> or any parts of it are used any other
 * packaged software, attribution needs to be given to the author, <author />.  This can be in the form of a textual
 * message at program startup or in documentation (online or textual) provided with the packaged software.
 * </agreement>
 * <product_url>http://www.managedfusion.com/products/url-rewriter/</product_url>
 * <license_url>http://www.managedfusion.com/products/url-rewriter/license.aspx</license_url>
 */

using System;
using System.Collections.Generic;
using ManagedFusion.Rewriter.Configuration;

namespace ManagedFusion.Rewriter.Engines
{
	public class ModuleFactory
	{
		private Dictionary<string, Type> _localModules;

		public ModuleFactory()
		{
			_localModules = new Dictionary<string, Type>(StringComparer.CurrentCultureIgnoreCase);
		}

		public void AddModule(string name, Type type)
		{
			_localModules.Add(name, type);
		}

		public bool ContainsName(string name)
		{
			return GetModule(name) != null;
		}

		public Type GetModule(string name)
		{
			// check the local modules
			if (_localModules.ContainsKey(name))
				return _localModules[name];

			// check the global modules
			for (int i = 0; i < Manager.Configuration.Rewriter.Modules.Count; i++)
			{
				RewriterModuleItem item = Manager.Configuration.Rewriter.Modules[i];

				if (String.Equals(name, item.Name, StringComparison.CurrentCultureIgnoreCase))
					return item.Type;
			}

			return null;
		}
	}
}
